package com.android.scanner.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

import com.android.scanner.R;
import com.android.scanner.adapter.BBListAdapter;
import com.android.scanner.utils.Constant;

public class BBScanFragment extends BaseFragment implements OnClickListener {

	private Handler handler;
	private static BBScanFragment mSelft = null;
	private Button btn_left, btn_middle, btn_right;

	private BBScanFragment(Handler handler) {
		this.handler = handler;
	}

	public static BBScanFragment getInstance(Handler handler) {
		if (mSelft != null)
			return mSelft;
		return new BBScanFragment(handler);
	}

	@Override
	public void onAttach(Activity activity) {
		// TODO Auto-generated method stub
		super.onAttach(activity);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.bb_scan, null);
		initView(view);
		initData();
		return view;
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	public void initView(View view) {
		// TODO Auto-generated method stub
		btn_left = (Button) view.findViewById(R.id.btn_left);
		btn_middle = (Button) view.findViewById(R.id.btn_middle);
		btn_right = (Button) view.findViewById(R.id.btn_right);
		btn_left.setOnClickListener(this);
		btn_right.setOnClickListener(this);

	}

	@Override
	public void initData() {
		// TODO Auto-generated method stub
		// super.initData();
	}


 	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.btn_left:
			handler.sendEmptyMessage(Constant.BB_OPTION);
			break;
		case R.id.btn_right:
			handler.sendEmptyMessage(Constant.BB_OPTION);
			break;

		default:
			break;
		}
	}

	@Override
	public boolean setKeyEvent(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		// Toast.makeText(context, "key Event is " + keyCode,
		// Toast.LENGTH_SHORT)
		// .show();
		if (keyCode == KeyEvent.KEYCODE_MENU) {
			btn_left.callOnClick();
		} else if (keyCode == KeyEvent.KEYCODE_BACK) {
			btn_right.callOnClick();
		} else if (keyCode == KeyEvent.KEYCODE_D) {

		} else if (keyCode == KeyEvent.KEYCODE_U) {

		} else if (keyCode == KeyEvent.KEYCODE_L) {
			btn_left.callOnClick();
		} else if (keyCode == KeyEvent.KEYCODE_R) {
			btn_middle.callOnClick();
		} else if (keyCode == KeyEvent.KEYCODE_M) {
			btn_right.callOnClick();
		}
		return super.setKeyEvent(keyCode, event);
	}

}
